// const base = require('./base');
// const { login } = require('./aliplugin');
import {
	base
} from './base.js'
import store from '../store/index.js'
import {
	v4 as uuidv4
} from 'uuid';
import md5 from "./md5.js"

function getCryptAccount(serial, token = '') {
	var str = JSON.stringify({
		serial,
		token
	});
	// 公钥
	var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMdIgPTzrPGM2UAG9iUSaR6uB8Pf7+m6pRv6Qe4GmR3TTvklmLnSuaQ2UH15lwyfjvu2dq1V6yY46bI8kl4pqVmHAxussFSEnQosq10y4Y4c1Rw3/62VWAd9JzVrE8ltZgeRwNdLtCYiuHHT2yhIktr9V2ac+Tg7t1t47tS2SA5QIDAQAB";
	// 加密
	// const encryptor = new Encrypt();
	// encryptor.setPublicKey(publicKey);
	// console.log(str + publicKey, 5555)
	return md5.hex_md5(str + publicKey);
}

function fetch(url, method, data = {}, header = {}) {
	const token = uni.getStorageSync("token") || '';
	const uuid = uuidv4()
	const sign = getCryptAccount(uuid, (token || ''))
	const serial = uuid

	return new Promise(function(resolve, reject) {
		uni.request({
			url: url.includes('http') ? url : base.url + url,
			header: token ? Object.assign(header, {
				token,
				tenantId: '46e8fb46ee4e4193940b26517a88fd55',
				sign,
				serial
			}) : Object.assign(header, {
				tenantId: '46e8fb46ee4e4193940b26517a88fd55',
				sign,
				serial
			}),
			data: data,
			method: method,
			success: function(res) {
				switch (res.statusCode) {
					case 200:
						if (res.data && res.data["code"] === "0000") {
							return resolve(res.data || null)
						} else {
							if (res.data["code"] !== '210030' && res.data["code"] != '310005') {
								uni.showToast({
									title: res.data["msg"] || "",
									icon: "none"
								});
							}
							return reject({
								code: res.data["code"],
								msg: res.data["msg"],
								data: res.data["data"]
							})
						}
					case 500:
						uni.showToast({
							title: "未知异常",
							icon: "none"
						});
						return reject({
							code: 500,
							msg: "未知异常",
							data: res.data
						})
					case 401:
						uni.removeStorageSync('token')
						uni.navigateTo({
							url: '/pages/index/index'
						})
						return reject({
							code: 401,
							msg: "用户未登录",
							data: res.data
						})
					default:
						if (url.includes('dapf-receive/receive/push')) {
							return resolve(res.data)
						}
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
						return resolve(res.data)
				}
			},
			fail: err => {
				if (err.statusCode == 401) {
					uni.removeStorageSync('token')
					uni.navigateTo({
						url: '/pages/index/index'
					})
				}
				return reject(err)
			}
		})

	});
}

function fetchForm(url, method, data = {}, header = {}) {
	const token = uni.getStorageSync("token") || '';
	const uuid = uuidv4()
	const sign = getCryptAccount(uuid, (token || ''))
	const serial = uuid

	return new Promise(function(resolve, reject) {

		uni.uploadFile({
			url: url,
			filePath: data.filePath,
			name: 'file',
			header: token ? Object.assign(header, {
				token,
				tenantId: '46e8fb46ee4e4193940b26517a88fd55',
				sign,
				serial
			}) : Object.assign(header, {
				tenantId: '46e8fb46ee4e4193940b26517a88fd55',
				sign,
				serial
			}),
			method: method,
			success: function(res) {
				switch (res.statusCode) {
					case 200:
						res.data = JSON.parse(res.data)
						if (res.data && res.data["code"] === "0000") {
							return resolve(res.data["data"] || null)
						} else {
							uni.showToast({
								title: res.data["msg"] || "",
								icon: "none"
							});
							return reject({
								code: res.data["code"],
								msg: res.data["msg"],
								data: res.data["data"]
							})
						}
					case 500:
						uni.showToast({
							title: "未知异常",
							icon: "none"
						});
						return reject({
							code: 500,
							msg: "未知异常",
							data: res.data
						})
					case 401:
						uni.removeStorageSync('token')
						uni.navigateTo({
							url: '/pages/index/index'
						})
						return reject({
							code: 401,
							msg: "用户未登录",
							data: res.data
						})
					default:
						uni.showToast({
							title: res.errMsg,
							icon: "none"
						});
						return resolve(res.data)
				}
			},
			fail: err => {
				if (err.statusCode == 401) {
					uni.removeStorageSync('token')
					uni.navigateTo({
						url: '/pages/index/index'
					})
				}
				return reject(err)
			}
		})

	});
}

function get(url, data = {}, header = {}) {
	return fetch(url, "GET", data, header);
}

function post(url, data = {}, header = {}) {
	return fetch(url, "POST", data, header);
}

function put(url, data = {}, header = {}) {
	return fetch(url, "PUT", data, header);
}

function deleteUrl(url, data = {}, header = {}) {
	return fetch(url, "DELETE", data, header);
}

function postUrl(url, data = {}, header = {}) {
	return fetchForm(url, "POST", data, header);
}
module.exports = {
	fetch,
	get,
	post,
	put,
	postUrl,
	deleteUrl
}